package com.seeyon.apps.util;

import com.seeyon.cap4.form.bean.*;
import com.seeyon.cap4.form.exception.FormException;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;

import org.apache.commons.logging.Log;

import java.util.List;
import java.util.Map;


/**
 * Description
 * <pre></pre>
 * @author FanGaowei<br>
 * Copyright(c) Beijing Seeyon Software Co.,LTD
 */
public class CAP4FormKit {
    
    private static final Log LOGGER = CtpLogFactory.getLog(CAP4FormKit.class);

    public static FormBean getFormBean(CAP4FormManager cap4FormManager, String code) {
        FormBean formBean = null;
        try {
            formBean = cap4FormManager.getFormByFormCode(code);
        } catch(BusinessException e) {
            LOGGER.error("获取表单发生异常,编号：" + code, e);
        }
        return formBean;
    }
	
	/**
	 * 根据表单的显示名称获取字段的值
	 * @param bean
	 * @param disPlay
	 * @return
	 */
	public static Object getFieldValue(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		if(field == null) {
			return null;
		}
		return bean.getFieldValue(field.getName());
	}
	public static <T> T getFieldValue(FormDataBean bean, String disPlay, Class<T> cls) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		if(field == null) {
			return null;
		}
		return Convert.convert(cls, bean.getFieldValue(field.getName()));
	}

    public static <T> T getFieldValue(FormDataBean bean, String disPlay, Class<T> cls, T defaultValue) {
        if(bean == null) {
            return defaultValue;
        }
        FormTableBean table = bean.getFormTable();
        if(table == null) {
            return defaultValue;
        }
        FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
        if(field == null) {
            return defaultValue;
        }
        return Convert.convert(cls, bean.getFieldValue(field.getName()), defaultValue);
    }

	/**
	 * 通过控件名称获取field00**
	 * @param fieldDisplay 控件名称
	 * @param formBean
	 * @return
	 */
	public static String getFieldName(String fieldDisplay, FormBean formBean) {
		FormFieldBean field = formBean.getFieldBeanByDisplay(fieldDisplay);
		if(field != null) {
			return field.getName();
		}
		return null;
	}
	
	/**
	 * 获取表单字段的显示值
	 * @param bean
	 * @param disPlay
	 * @return
	 */
	public static String getFieldDisplayValue(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		if(field == null) {
			return null;
		}
		return getFieldDisplayValue(bean, field);
	}
	
	public static String getFieldDisplayValue(FormDataBean bean, FormFieldBean field) {
		if(field == null) {
			return null;
		}
		try {
			Object[] o = field.getDisplayValue(bean.getFieldValue(field.getName()));
			return StrKit.str(o[1]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return StrKit.str(bean.getFieldValue(field.getName()));
	}
	
	
	public static int getIntValue(FormDataBean bean, String disPlay) {
        Object value = getFieldValue(bean, disPlay);
        return StrKit.toInteger(value);
    }
	
	public static String getFieldStrValue(FormDataBean bean, String disPlay) {
	    Object value = getFieldValue(bean, disPlay);
	    return StrKit.str(value);
	}
	
	public static Object getFieldValueByName(FormDataBean bean, String fieldName) {
        return bean.getFieldValue(fieldName);
    }
	
	/**
	 * 根据表单的显示名称获取字段是  field000？
	 * @param bean
	 * @param disPlay
	 * @return
	 */
	public static String getFieldTaleId(FormBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getMasterTableBean();
		return getFieldTaleId(table, disPlay);
	} 
	
	/**
	 * 直接根据table来获取
	 * @param table
	 * @param disPlay
	 * @return
	 */
	public static String getFieldTaleId(FormTableBean table, String disPlay) {
        if(table == null) {
            return null;
        }
        FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
        if(field == null) {
            return null;
        }
        return field.getName();
    }
	
	public static String getFieldTaleId(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		if(field == null) {
			return null;
		}
		return field.getName();
	}
	
	public static FormFieldBean getFieldBean(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		return table.getFieldBeanByDisplay(disPlay);
	}
	
	public static FormFieldBean getFieldBean(FormTableBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		return bean.getFieldBeanByDisplay(disPlay);
	}

	public static FormFieldBean getFieldBean(FormBean bean, String disPlay) {
		Assert.notNull(bean, "表单bean不能为空");
		FormFieldBean formFieldBean = bean.getFieldBeanByDisplay(disPlay);
		Assert.notNull(formFieldBean, "表单【{}】没有定义【{}】字段", bean.getFormName(), disPlay);
		return formFieldBean;
	}
	
	
	/**
	 * Description:
	 * <pre></pre>
	 * @param bean 这里的bean必须是getMasterBean() 方法获取到的bean
	 * @param disPlay 
	 * @param value
	 */
	public static void setCellValue(FormDataBean bean, String disPlay, Object value) {
		FormFieldBean cell = CAP4FormKit.getFieldBean(bean, disPlay);
		if(cell != null) {
			bean.addFieldValue(cell.getName(), value);
		}
	}
	
	public static void setCellValue(FormDataSubBean bean, String disPlay, Object value) {
        FormFieldBean cell = CAP4FormKit.getFieldBean(bean, disPlay);
        if(cell != null) {
            bean.addFieldValue(cell.getName(), value);
        }
    }
	
	/**
	 * Description:
	 * <pre>只适用于只有一个子表的表单</pre>
	 * @param master
	 * @return
	 * @throws Exception
	 */
	public static List<FormDataSubBean> getSubBeans(FormDataMasterBean master) throws Exception {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		if(null != subs && subs.size() > 0) {
			for(String key : subs.keySet()) {
				return subs.get(key);
			}
		}
		return null;
	}
	
	/**
	 * 根据表名获取
	 * @param master
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public static List<FormDataSubBean> getSubBeansByTbName(FormDataMasterBean master, String tableName) throws Exception {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		return subs.get(tableName);
	}
	
	/**
	 * 根据显示名称获取数据
	 * @param master
	 * @param display	显示名称
	 * @return	
	 * @throws Exception
	 */
	public static List<FormDataSubBean> getSubBeans(FormDataMasterBean master, String display) throws BusinessException {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		if(null != subs && subs.size() > 0) {
			for(String key : subs.keySet()) {
				if(display.equals(subs.get(key).get(0).getFormTable().getDisplay()))
				return subs.get(key);
			}
		}
		return null;
	}
	
	/**
	 * Description:
	 * <pre>获取从表字段</pre>
	 * @param sub
	 * @param disPlay
	 * @return
	 */
	public static Object getSubFieldValue(FormDataSubBean sub, String disPlay) {
		return getFieldValue(sub, disPlay);
	}

	/**
	 * 根据子表的名称获取子表的tableBean
	 * @param formBean
	 * @param disPlay
	 * @return
	 */
	public static FormTableBean getSubTableBeanByDisplay(FormBean formBean, String disPlay) {
		return formBean.getSubTableBean().stream()
				.filter(item -> disPlay.equals(item.getDisplay()))
				.findFirst()
				.orElseThrow(() -> new FormException(String.format("【%s】表单中不存在【%s】子表单", formBean.getMasterTableBean().getDisplay(), disPlay)));
	}
}
